import Levels from './rule-level'
import Types from './rule-type'
import Utils from './_utils'
import _ from 'lodash'

const rule = 'number'
const defaultPattern = /^[-+]?[0-9]+(\.[0-9]+)?$/

/*
* config {
*   number: /^[-+]?[0-9]+(\.[0-9]+)?$/ // Function/String/RegExp Default float validation
* }
* */

export default {
  priority: Levels.MEDIUM_LEVEL,
  validate: function (value, config, model, formModel, filed, formSchema) {
    return {
      valid: Utils.validateWrapper(config.number, defaultPattern, value, config, model, formModel, (params, value, model, formModel) => {
        return _.isNil(value) || value === '' || params.test(value)
      }),
      errorTemplate: undefined,
      errorParams: undefined
    }
  },
  rule: rule,
  type: Types.ATTRIBUTE
}
